/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TableModel;

/**
 *
 * @author Adrian
 */
import Connection.Koneksi;
import Entities.Kendaraan;
import Entities.Servis;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import sun.java2d.pipe.SpanShapeRenderer.Simple;

public class Servis_List {

    private static Connection connection;
    private static Servis_List servis_list;

    public Servis_List(Connection connection) {
        //inisialisasi conn
        this.connection = connection;
    }

    public static Servis_List getServis_List() {
        //untuk mengambil status koneksi dari kelas Koneksi
        if (servis_list == null) {
            servis_list = new Servis_List(Koneksi.getConnection());
        }
        return servis_list;
    }

    public List<Servis> getServisbyKendaraan(String no_Polisi) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        connection.setAutoCommit(false);
        String sql = "select kode_servis,tanggal_servis,Status_oli,Catatan_servis,Biaya_servis,No_polisi from servis where no_polisi like '" + no_Polisi + "'";

        preparedStatement = connection.prepareStatement(sql);
        resultSet = preparedStatement.executeQuery();
        List<Servis> servises = new ArrayList<Servis>();

        while (resultSet.next()) {
            Servis servis = new Servis();
            boolean status_Oli;
            if (resultSet.getString(3).matches("true")) {
                status_Oli = true;
            }else {
                status_Oli = false;
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yy");
            servis.setKode_servis(resultSet.getString(1));
            servis.setTanggal_servis(simpleDateFormat.format(resultSet.getDate(2)));
            servis.setStatus_oli(status_Oli);
            servis.setCatatan_servis(resultSet.getString(4));
            servis.setBiaya_servis(resultSet.getDouble(5));
            servis.setNo_polisi(resultSet.getString(6));

            servises.add(servis);
        }
        connection.commit();
        return servises;
    }
}
